<template>
  <!-- 全局配置国际化设置的组件，设置语言的 -->
  <el-config-provider :locale="locale">
    <!-- 头部查询框的 -->
    <div class="flex-index">
      <el-form :model="searchForm" inline size="large">
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入商品名称进行查询"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearch"
            icon="Search"
            type="primary"
            size="large"
            >查询</el-button
          >
          <el-button
            @click="handleReset"
            v-if="backHome"
            icon="House"
            size="large"
            >回到首页</el-button
          >
        </el-form-item>
      </el-form>
      <el-button @click="handleCreate" icon="CirclePlus">新增商品</el-button>
    </div>
    <!-- 表格数据 -->
    <el-table
      :data="tableData"
      border
      class="table"
      v-loading="loading"
      empty-text="暂无数据"
      stripe
      highlight-current-row
      size="large"
      fit
      style="font-size: 16px"
    >
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="单价"></el-table-column>
      <el-table-column prop="level" label="库存"></el-table-column>
      <el-table-column
        prop="onSale"
        label="是否上架"
        width="150px"
        align="center"
        :filters="[
          { text: '是', value: '是' },
          { text: '否', value: '否' },
        ]"
        :filter-method="filterHandler"
      ></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column
        prop="createTime"
        label="添加时间"
        sortable
      ></el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        resizable="true"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            @click="handleEdit(row)"
            icon="Edit"
            type="primary"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(row)"
            icon="delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页和列表查询功能 -->
    <el-pagination
      v-model:currentPage="currentPageNum"
      :page-sizes="[10, 15]"
      :page-size="pageSize"
      layout="->,total,sizes,prev,pager,next,jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      class="pagination"
      background
    >
    </el-pagination>
    <!-- 弹出的对话框组件 -->
    <Dialog
      v-if="show"
      :title="tl"
      :upDateData="upDateData"
      @closeDialog="closeDialog"
    >
    </Dialog>
  </el-config-provider>
</template>

<script>
import { ElMessageBox } from 'element-plus'
import { listPart, searchData, deleteData } from '@/apis/data/userData'
import { formatTime, formatterOnsale } from '@/utils/util'
import Dialog from './dialog.vue'
export default {
  name: 'App',
  components: { Dialog },
  data() {
    return {
      backHome: false,
      currentPageNum: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      loading: true,
      show: false,
      tl: '',
      upDateData: null,
      searchForm: {
        name: '',
      },
      formRef: '',
    }
  },
  created() {
    this.getData(1, this.pageSize)
  },
  methods: {
    handleReset() {
      this.backHome = false
      this.getData(1, this.pageSize)
    },
    handleSizeChange(val) {
      this.loading = true
      this.pageSize = val
      this.getData(this.currentPageNum, val)
    },
    handleCurrentChange(val) {
      this.loading = true
      this.currentPageNum = val
      this.getData(val, this.pageSize)
    },
    getData(pageNum, pagesize) {
      // 这里就需要掉后端接口去拿数据了
      listPart(pageNum, pagesize)
        .then((res) => {
          // 写一个请求拦截和响应拦截
          res.dataList.forEach((item) => {
            item.createTime = formatTime(item.createTime)
            item.onSale = formatterOnsale(item.onSale)
          })
          this.tableData = res.dataList
          this.total = res.totalCount
          this.loading = false
        })
        .catch((err) => {
          // 错误提示
          console.log(err)
        })
        .finally(() => {})
    },
    handleSearch() {
      this.loading = true
      searchData(this.searchForm.name)
        .then((res) => {
          if (res.code === -5) {
            this.loading = false
            this.$message({
              message: '该商品信息未查到！',
              type: 'waring',
              showClose: true,
            })
          } else if (res.code == 200) {
            this.loading = false
            let goodsItem = res.dataList
            goodsItem.createTime = formatTime(goodsItem.createTime)
            goodsItem.onSale = formatterOnsale(goodsItem.onSale)
            this.tableData = []
            this.tableData.unshift(goodsItem)
            this.total = 1
            this.backHome = true
          }
        })
        .catch((err) => {
          console.log(err)
        })
        .finally(() => {})
      console.log(this.searchForm.name)
    },
    handleCreate() {
      this.show = true
      this.tl = '新增商品'
    },
    // 删除操作
    handleDelete(row) {
      ElMessageBox.confirm(`确定删除该商品：${row.name}?`, '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          deleteData(row.ID)
            .then((res) => {
              if (res.code === -5) {
                this.$message({
                  message: '删除商品失败',
                  type: 'waring',
                  showClose: true,
                })
              } else if (res.code == 200) {
                this.getData(1, this.pageSize)
                this.$message({
                  message: '删除商品成功',
                  type: 'success',
                  showClose: true,
                })
              }
            })
            .catch((err) => {
              console.log(err)
            })
            .finally(() => {
              console.log('输出成功！')
            })
        })
        .catch(() => {
          console.log('用户取消了删除')
        })
    },
    handleEdit(row) {
      this.tl = '编辑商品'
      this.show = true
      this.upDateData = row
      console.log(this.upDateData)
    },
    filterHandler(value, row, column) {
      console.log(value, row, column)
      const property = column['property']
      return row[property] === value
    },
    closeDialog() {
      this.upDateData=null
      this.show = false
      this.getData(1, this.pageSize)
    },
  },
}
</script>

<style lang="less">
.flex-index {
  display: flex;
  width: 95%;
  justify-content: space-between;
  align-items: center;
  color: #2c3e50;
  margin: 20px auto;
  margin-bottom: 0px;
  font-weight: 600;
  font-size: 20px;
}
.table {
  width: 95%;
  margin: 0px auto;
}
.pagination {
  width: 100%;
  margin: 20px 20px;
}
</style>
